package com.zhangyu.shadow.ec.main.sort.list;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tepth.shadow.ui.recycler.DataConverter;
import com.tepth.shadow.ui.recycler.ItemType;
import com.tepth.shadow.ui.recycler.MultipleField;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * @author zhangyu
 * @date 2017/11/7
 */

public final class VerticalListDataConverter extends DataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray jsonArray = JSON.parseObject(getJsonData())
                .getJSONObject("data")
                .getJSONArray("list");
        final int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject object = jsonArray.getJSONObject(i);
            final int id = object.getInteger("id");
            final String name = object.getString("name");
            MultipleItemEntity itemEntity = MultipleItemEntity.builder()
                    .setItemType(ItemType.VERTICAL_MENU_LIST)
                    .setField(MultipleField.ID, id)
                    .setField(MultipleField.NAME, name)
                    .setField(MultipleField.TAG, false)
                    .build();
            dataList.add(itemEntity);
            //设置第一个选中状态
            dataList.get(0).setField(MultipleField.TAG, true);
        }
        return dataList;
    }
}
